home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / net / dgcli.c next >
C/C++ Source or Header  |  1989-12-17  |  1KB  |  45 lines

  1. /*
  2.  * Read the contents of the FILE *fp, write each line to the
  3.  * datagram socket, then read a line back from the datagram
  4.  * socket and write it to the standard output.
  5.  *
  6.  * Return to caller when an EOF is encountered on the input file.
  7.  */
  8.  
  9. #include    <stdio.h>
  10. #include    <sys/types.h>
  11. #include    <sys/socket.h>
  12.  
  13. #define    MAXLINE    512
  14.  
  15. dg_cli(fp, sockfd, pserv_addr, servlen)
  16. FILE        *fp;
  17. int        sockfd;
  18. struct sockaddr    *pserv_addr;    /* ptr to appropriate sockaddr_XX structure */
  19. int        servlen;    /* actual sizeof(*pserv_addr) */
  20. {
  21.     int    n;
  22.     char    sendline[MAXLINE], recvline[MAXLINE + 1];
  23.  
  24.     while (fgets(sendline, MAXLINE, fp) != NULL) {
  25.         n = strlen(sendline);
  26.         if (sendto(sockfd, sendline, n, 0, pserv_addr, servlen) != n)
  27.             err_dump("dg_cli: sendto error on socket");
  28.  
  29.         /*
  30.          * Now read a message from the socket and write it to
  31.          * our standard output.
  32.          */
  33.  
  34.         n = recvfrom(sockfd, recvline, MAXLINE, 0,
  35.                 (struct sockaddr *) 0, (int *) 0);
  36.         if (n < 0)
  37.             err_dump("dg_cli: recvfrom error");
  38.         recvline[n] = 0;    /* null terminate */
  39.         fputs(recvline, stdout);
  40.     }
  41.  
  42.     if (ferror(fp))
  43.         err_dump("dg_cli: error reading file");
  44. }
  45.